﻿using Microsoft.AspNetCore.Http;
using ProTrack.Entity;
using ProTrack.IServices;
using ProTrack.IServices.SystemDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace ProTrack.Api.Helper
{
    /// <summary>
    /// 系统日志收集辅助类
    /// </summary>
    public class LogHelper
    {
        private readonly ILogServices logServices;
        private readonly IAccountServices accountServices;

        public LogHelper(ILogServices logServices,IAccountServices accountServices)
        {
            this.logServices = logServices;
            this.accountServices = accountServices;
        }




        /// <summary>
        /// 写入请求错误日志
        /// </summary>
        /// <param name="ApplicationName">功能名称</param>
        /// <param name="ex">错误模型</param>
        /// <param name="context">请求上下文</param>
        /// <returns></returns>
        public  async Task<bool> WriteErrorLog(string ApplicationName,Exception ex, HttpContext context = null)
        {
            try
            {
                //获取错误详细信息
                var exModel = ExceptionHelper.GetExceptionDetails(ex);
                var RequestUrl = context == null ? "无" : context.Request.Path.ToString();
                var UserAgent = context == null ? "无" : context.Request.Headers["User-Agent"].ToString();

                //获取ip
                string IPAddress = context.Connection.RemoteIpAddress.MapToIPv4().ToString();
                if (string.IsNullOrEmpty(IPAddress))
                {
                    context.Connection.RemoteIpAddress.ToString();
                }

                int UserId = await accountServices.GetUserIdByUserName(context.User.Identity.Name);

                var model = new InfraApiErrorLog {
                    TraceId = Guid.NewGuid().ToString(),
                    UserId= UserId,
                    ApplicationName= ApplicationName,
                    RequestMethod=exModel.MethodName,
                    RequestUrl= RequestUrl,
                    RequestType= context.Request.Method,
                    RequestParams =exModel.ParameterInfo,
                    UserIP = IPAddress,
                    UserAgent= UserAgent,
                    ExceptionTime=exModel.ExceptionTime,
                    ExceptionName= ex.GetType().Name,
                    ExceptionMessage=exModel.ExceptionMessage,
                    ExceptionRootCauseMessage=exModel.RootExceptionMessage,
                    ExceptionStackTrace=exModel.StackTraceStr,
                    ExceptionClassName = exModel.ClassName,
                    ExceptionFileName=exModel.FileName,
                    ExceptionMethodName=exModel.MethodName,
                    ExceptionLineNumber=exModel.LineNumber,
                    CreateTime= exModel.ExceptionTime,
                    ProcessTime= exModel.ExceptionTime,
                    Creator= "",
                    Deleted=false,
                    ProcessStatus=0,
                    ProcessUserId=0,
                    UpdateTime= exModel.ExceptionTime,
                    Updater=""


                };
                return await logServices.WriteErrorLog(model);
            }
            catch (Exception exInfo)
            {
                //写入日志异常
                Console.WriteLine(exInfo.Message);
            }
            return false;
        }
    }
}
